﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medical.Business;
using System.Data;
using Medical.Entity;
using System.Text;

/// <summary>
/// 文章详细页面
/// </summary>
public partial class ArticleInfo : WebPage
{
    /// <summary>
    /// 文章类型
    /// </summary>
    protected ArticelTypeEnum eType;

    protected int nSortID = 0;

    protected int nCount = 1;

    protected int nAllCount = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        InitConfig();
        if (!Page.IsPostBack)
        {
            InitData();
        }
    }

    /// <summary>
    /// 数据初始化
    /// </summary>
    protected void InitConfig()
    {
        if (RouteData.Values["urlname"] != null)
        {
            string strUrlName = RouteData.Values["urlname"].ToString();
            DataTable dtSort = new BisSortInfo().GetSortInfo(strUrlName);
            if (dtSort != null && dtSort.Rows.Count > 0)
            {
                nSortID = Convert.ToInt32(dtSort.Rows[0]["ID"]);
                int nPSortID = nSortID;
                if (dtSort.Rows[0]["PID"].ToString() != string.Empty)
                {
                    nPSortID = Convert.ToInt32(dtSort.Rows[0]["PID"]);
                    ltlPageUrl.Text = "<a href=\"/" + dtSort.Rows[0]["PPageUrlName"].ToString() + "/\" title=\"" + dtSort.Rows[0]["PName"].ToString() + "\" target=\"_blank\">" + Server.HtmlEncode(dtSort.Rows[0]["PName"].ToString()) + "</a> &gt; ";
                }
                eType = (ArticelTypeEnum)Convert.ToInt32(dtSort.Rows[0]["Type"]);
                if (eType == ArticelTypeEnum.Equipment)
                {
                    ltlPageUrl.Text += "<a href=\"/" + dtSort.Rows[0]["PageUrlName"].ToString() + "/\" title=\"先进设备\" target=\"_blank\">先进设备</a>";
                }
                else if (eType == ArticelTypeEnum.Subject)
                {
                    ltlPageUrl.Text += "<a href=\"/topic/\" title=\"精彩专题\" target=\"_blank\">精彩专题</a>";
                }
                else if (eType == ArticelTypeEnum.About)
                {
                    ltlPageUrl.Text += "<a href=\"/about/\" title=\"医院概况\" target=\"_blank\">医院概况</a>";
                }
                else if (eType == ArticelTypeEnum.Expert)
                {
                    ltlPageUrl.Text += "<a href=\"/doctor/\" title=\"权威专家\" target=\"_blank\">权威专家</a>";
                }
                else
                {
                    ltlPageUrl.Text += "<a href=\"/" + (dtSort.Rows[0]["PID"].ToString() != string.Empty ? (dtSort.Rows[0]["PPageUrlName"].ToString() + "/") : "") + dtSort.Rows[0]["PageUrlName"].ToString() + "/\" title=\"" + dtSort.Rows[0]["Name"].ToString() + "\" target=\"_blank\">" + Server.HtmlEncode(dtSort.Rows[0]["Name"].ToString()) + "</a>";
                }
                StringBuilder sbHtml = new StringBuilder();
                DataTable dtArticle = new BisArticle().GetIsHotArticle(9, eType, nPSortID);
                foreach (DataRow drArticle in dtArticle.Rows)
                {
                    sbHtml.AppendFormat(
                        "<span><a href=\"{0}\" title=\"{1}\" target=\"_blank\">{2}</a></span>",
                        Until.GetUrlName(drArticle["PSortUrlName"].ToString(), drArticle["SortUrlName"].ToString(), drArticle["ID"].ToString()),
                        drArticle["Name"].ToString(),
                        Server.HtmlEncode(drArticle["Name"].ToString()));
                }
                int nAllCount = 9 - dtArticle.Rows.Count;
                for (int i = 0; i < nAllCount; i++)
                {
                    sbHtml.Append("<span></span>");
                }
                tag.InnerHtml = sbHtml.ToString();
                //rptArticle.DataSource = dtArticle;
                //rptArticle.DataBind();
            }
            else
            {
                Response.Redirect("/404.html");
            }
        }
        else
        {
            Response.Redirect("/404.html");
        }
    }

    /// <summary>
    /// 获取文章信息
    /// </summary>
    protected void InitData()
    {
        if (RouteData.Values["id"] != null)
        {
            BisArticle bisArticle = new BisArticle();
            int nID = Convert.ToInt32(RouteData.Values["id"]);
            Article Info = bisArticle.Get(nID);
            if (Info != null)
            {
                ltlTitle.Text = Server.HtmlEncode(Info.Name);
                ltlInfo.Text = Info.Info;
                ltlTime.Text = Info.AddTime.Value.ToString("yyyy-MM-dd");
                ltlTouch.Text = Info.Touch.ToString();
                ltlPageUrl.Text += " > "+Server.HtmlEncode(Info.Name);
                if (eType == ArticelTypeEnum.Expert || eType == ArticelTypeEnum.Equipment)
                {
                    aboutimg.Visible = true;
                    Img.ImageUrl = "/" + WebConfigInfo.FilePlace + "d/" + Info.ImgPlace;
                    Img.ToolTip = Info.Name;
                }
                if (eType == ArticelTypeEnum.Expert || eType == ArticelTypeEnum.Equipment || eType == ArticelTypeEnum.Case)
                {
                    DataTable dtArticelMate = new BisArticleMetaValue().GetList(eType, nID);
                    if (dtArticelMate!=null&&dtArticelMate.Rows.Count > 0)
                    {
                        rptMeta.DataSource = dtArticelMate;
                        rptMeta.DataBind();
                        aboutmeta.Visible = true;
                    }
                }
                bisArticle.SetTouch(Info.ID.Value);
                string strPageTitle = string.IsNullOrEmpty(Info.PageTitle) ? (Info.Name + "_" + strWebTitle) : Info.PageTitle;
                SetPageInfo(strPageTitle, Info.PageKeywords, Info.PageDescription);
            }

            DataTable dtArticle1 = bisArticle.GetTopArticle(10, eType, nSortID);
            nAllCount = dtArticle1.Rows.Count;
            rptArticle1.DataSource = dtArticle1;
            rptArticle1.DataBind();

            DataTable dtUpArticle = bisArticle.GetArticle(nID, nSortID, 1);
            if (dtUpArticle != null && dtUpArticle.Rows.Count > 0)
            {
                ltlUpArticle.Text = "<a href=\"" + Until.GetUrlName(dtUpArticle.Rows[0]["PSortUrlName"], dtUpArticle.Rows[0]["SortUrlName"], dtUpArticle.Rows[0]["ID"]) + "\" target=\"_blank\" title=\"" + dtUpArticle.Rows[0]["Name"].ToString() + "\">" + Server.HtmlEncode(dtUpArticle.Rows[0]["Name"].ToString()) + "</a>";
            }
            else
            {
                ltlUpArticle.Text = "无";
            }
            DataTable dtDownArticle = bisArticle.GetArticle(nID, nSortID, 0);
            if (dtDownArticle != null && dtDownArticle.Rows.Count > 0)
            {
                ltlDownArticle.Text = "<a href=\"" + Until.GetUrlName(dtDownArticle.Rows[0]["PSortUrlName"], dtDownArticle.Rows[0]["SortUrlName"], dtDownArticle.Rows[0]["ID"]) + "\" target=\"_blank\" title=\"" + dtDownArticle.Rows[0]["Name"].ToString() + "\">" + Server.HtmlEncode(dtDownArticle.Rows[0]["Name"].ToString()) + "</a>";
            }
            else
            {
                ltlDownArticle.Text = "无";
            }

            //DataTable dtBanner = new BisBanner().GetList(AdverTypeEnum.ArticleBanner);
            //string strBanner = string.Empty;
            //if (dtBanner != null && dtBanner.Rows.Count > 0)
            //{
            //    if (!string.IsNullOrEmpty(dtBanner.Rows[0]["Url"].ToString()))
            //    {
            //        strBanner = "<a href=\"" + dtBanner.Rows[0]["Url"].ToString() + "\" title=\"" + dtBanner.Rows[0]["Name"].ToString() + "\" target=\"_blank\">";
            //    }
            //    strBanner += "<img src=\"/" + WebConfigInfo.FilePlace + "d/" + dtBanner.Rows[0]["ImgPlace"].ToString() + "\" alt=\"" + dtBanner.Rows[0]["Name"].ToString() + "\" />";
            //    if (!string.IsNullOrEmpty(dtBanner.Rows[0]["Url"].ToString()))
            //    {
            //        strBanner += "</a>";
            //    }
            //}
            //ltlArticleImg.Text = strBanner;
        }
        else
        {
            Response.Redirect("/404.html");
        }
    }

    protected string getli(int nType)
    {
        if (nCount % 4 == 1 && nType == 0)
        {
            return "<li>";
        }
        if (nType == 1)
        {
            if (nCount % 4 == 0 || nAllCount == nCount)
            {
                nCount++;
                return "</li>";
            }
            else
            {
                nCount++;
            }
        }
        return "";
    }
}